from ninja import ModelSchema
from ninja.schema import Schema, Field, model_validator
from ninja_extra.exceptions import APIException

from goods.models import SKU


class CartSchemaIn(Schema):
    """购物车架构"""
    sku_id: int
    count: int = 1
    selected: bool = True

    @model_validator(mode='after')
    def check_sku_id(self):
        if not SKU.objects.filter(id=self.sku_id).exists():
            raise APIException('商品数据有误，请重试')
        return self


class CartSKUSchema(ModelSchema):
    """
    ModelSchema:自定义字段加上后不需要在fields中添加
    """
    count: int
    selected: bool

    class Meta:
        model = SKU
        fields = ['id', 'name', 'price', 'default_image_url']


class CartSelectSchema(Schema):
    selected: bool
